using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OA.BLL
{
    /// <summary>
    /// 个人
    /// </summary>
    public class Personal:Base
    {
        public Personal()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        public Common.Result ChangePassword(string oldPassword, string newPassword)
        {
            Common.Result ret = new Common.Result(false, 0, "");
            var dal = new DAL.Manager(isession);
            var model = dal.GetModel(LoginUser.LoginManager.UserName);
            if (model == null)
            {
                ret.message = "用户不存在";
                return ret;
            }
            if (model.Password != Common.Encrypt.MD5.MD5_32(oldPassword))
            {
                ret.message = "当前密码错误";
                return ret;
            }
            model.Password = Common.Encrypt.MD5.MD5_32(oldPassword);
            ret = dal.Update(model);
            if (ret.success)
            {
                LoginUser.LoginManager = model;
                LoginUser.SetManagerToCookies(model);
            }
            return ret;
        }

        /// <summary>
        /// 修改资料
        /// </summary>
        public Common.Result ChangeData(Model.Manager value)
        {
            Common.Result ret = new Common.Result(false, 0, "");
            var dal = new DAL.Manager(isession);
            var model = dal.GetModel(LoginUser.LoginManager.UserName);
            if (model == null)
            {
                ret.message = "用户不存在";
                return ret;
            }
            model.RealName = value.RealName;
            model.GroupID = value.GroupID;
            model.Telephone = value.Telephone;
            model.Email = value.Email;
            ret = dal.Update(model);
            if (ret.success)
            {
                LoginUser.LoginManager = model;
                LoginUser.SetManagerToCookies(model);

            }
            return ret;
        }
    }
}
